﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyIni;

namespace CadStrategy2013.CgqStyle
{
    /// <summary>
    /// 通过对表格的编辑，生成 统计.ini文件
    /// </summary>
    public partial class CommuCfgForm : Form
    {
        private string m_path;
        private string m_stationName;
        private int zlbsCount;
        private int zldlCount;
        private int jlbsCount;
        private int jldlCount;
        private int ypfsCount;
        private int ypjsCount;
        private int dmhCount;
        private int gybdcCount;
        private int bzdCount;
        private int zlCount;

        private int d_zlbsCount;
        private int d_zldlCount;
        private int d_jlbsCount;
        private int d_jldlCount;
        private int d_ypfsCount;
        private int d_ypjsCount;
        private int d_dmhCount;
        private int d_gybdcCount;
        private int d_bzdCount;
        private int d_zlCount;

        public Dictionary<string, string> zldcbsDic = new Dictionary<string, string>();
        public Dictionary<string, string> zldcdlDic = new Dictionary<string, string>();
        public Dictionary<string, string> jldcbsDic = new Dictionary<string, string>();
        public Dictionary<string, string> jldcdlDic = new Dictionary<string, string>();
        public Dictionary<string, string> ypfsDic = new Dictionary<string, string>();
        public Dictionary<string, string> ypjsDic = new Dictionary<string, string>();
        public Dictionary<string, string> dmhDic = new Dictionary<string, string>();
        public Dictionary<string, string> zlDic = new Dictionary<string, string>();
        public Dictionary<string, string> bzdDic = new Dictionary<string, string>();
        public Dictionary<string, string> gybdcDic = new Dictionary<string, string>();

        private Dictionary<string, int> cgqCountDic = new Dictionary<string, int>();
        public DataTable dataTable1;
        public DataTable dataTable2;
        public CommuCfgForm()
        {
            m_path = "./";
            IniFile ini = new IniFile("./\\全局.ini");
            m_stationName = ini.GetString("全局", "站名", "XXX");
            InitializeComponent();
            SetCgqCount();
            UpdateCgqCount();
            dataTable1 = new DataTable();
            dataTable2 = new DataTable();
            dataTable1.Columns.Add("序号", typeof(int));
            dataTable1.Columns.Add("端口1", typeof(string));
            dataTable1.Columns.Add("端口2", typeof(string));
            dataTable1.Columns.Add("端口3", typeof(string));
            dataTable1.Columns.Add("端口4", typeof(string));
            dataTable2.Columns.Add("序号", typeof(int));
            dataTable2.Columns.Add("端口1", typeof(string));
            dataTable2.Columns.Add("端口2", typeof(string));
            dataTable2.Columns.Add("端口3", typeof(string));
            dataTable2.Columns.Add("端口4", typeof(string));

            for (int i = 0; i < 32; i++)
            {
                DataRow row = dataTable1.NewRow();
                row["序号"] = i + 1;
                dataTable1.Rows.Add(row);
                DataRow row1 = dataTable2.NewRow();
                row1["序号"] = i + 1;
                dataTable2.Rows.Add(row1);
            }
            dataGridView1.DataSource = dataTable1;
            dataGridView2.DataSource = dataTable2;
            dataTable1.Columns["序号"].ReadOnly = true;
            dataGridView1.Columns["序号"].Frozen = true;
            dataGridView1.Columns["序号"].Width = 40;
           
        }

        public CommuCfgForm(string path, string stationName)
        {
            m_path = path;
            m_stationName = stationName;
            InitializeComponent();
            SetCgqCount();
            UpdateCgqCount();
            dataTable1 = new DataTable();
            dataTable2 = new DataTable();
            for (int i = 0; i < 32; i++)
            {
                DataRow row = dataTable1.NewRow();
                row[0] = i + 1;

            }
            dataTable1.Columns["序号"].ReadOnly = true;
            // dataTable1.Columns[0].
            dataGridView1.DataSource = dataTable1;
            dataGridView1.Columns["序号"].Frozen = true;
        }

        private void UpdateCgqCount()
        {
            GetCgqCountInGrid();
            zlbs.Text = (zlbsCount-d_zlbsCount) .ToString();
            if (zlbsCount - d_zlbsCount < 0)
                zlbs.BackColor = Color.Red;
            else
                zlbs.BackColor = Control.DefaultBackColor;
            zldl.Text = (zldlCount-d_zldlCount) .ToString();
            if (zldlCount - d_zldlCount < 0)
                zldl.BackColor = Color.Red;
            else
                zldl.BackColor = Control.DefaultBackColor;
            jlbs.Text = (jlbsCount-d_jlbsCount) .ToString();
            if (jlbsCount - d_jlbsCount < 0)
                jlbs.BackColor = Color.Red;
            else
                jlbs.BackColor = Control.DefaultBackColor;
            jldl.Text = (jldlCount-d_jldlCount) .ToString();
            if (jldlCount - d_jldlCount < 0)
                jldl.BackColor = Color.Red;
            ypfs.Text = (ypfsCount-d_ypfsCount) .ToString();
            if (ypfsCount - d_ypfsCount < 0)
                ypfs.BackColor = Color.Red;
            else
                ypfs.BackColor = Control.DefaultBackColor;
            ypjs.Text = (ypjsCount-d_ypjsCount) .ToString();
            if (zlbsCount - d_zlbsCount < 0)
                zlbs.BackColor = Color.Red;
            dmh.Text = (dmhCount-d_dmhCount).ToString();
            if (dmhCount - d_dmhCount < 0)
                dmh.BackColor = Color.Red;
            else
                dmh.BackColor = Control.DefaultBackColor;
            gybdc.Text = (gybdcCount-d_gybdcCount).ToString();
            if (gybdcCount - d_gybdcCount < 0)
                gybdc.BackColor = Control.DefaultBackColor;
            else
                gybdc.BackColor = Control.DefaultBackColor;
            bzd.Text = (bzdCount-d_bzdCount).ToString();
            if (bzdCount - d_bzdCount < 0)
                bzd.BackColor = Color.Red;
            else
                bzd.BackColor = Control.DefaultBackColor;
            zl.Text = (zlCount-d_zlCount).ToString();
            if (zlCount - d_zlCount < 0)
                zl.BackColor = Color.Red;
            else
                zl.BackColor = Control.DefaultBackColor;
        }

        private void SetCgqCount()
        {
            SetBzdZlCount();
            SetDcCount();
            SetYpCount();
            SetDmhCount();
            SetGybdcCount();
        }

        private void SetBzdZlCount()
        {
            IniFile inifile = new IniFile(m_path + "\\" + m_stationName + "\\bzdzl.txt");
            inifile.Load();
            bzdCount = (inifile.GetInt("半自动", "sum", 0) + 3) / inifile.GetInt("半自动", "路数", 4);
            zlCount = (inifile.GetInt("站联", "sum", 0) + 5) / inifile.GetInt("站联", "路数", 6);
        }

        private void SetDcCount()
        {
            IniFile inifile = new IniFile(m_path + "\\" + m_stationName + "\\dc.txt");
            inifile.Load();
            zlbsCount = zldlCount = (inifile.GetInt("直流道岔", "sum", 0) + 3) / inifile.GetInt("直流道岔", "路数", 4);
            jlbsCount = jldlCount = (inifile.GetInt("交流道岔", "sum", 0) + 1) / inifile.GetInt("交流道岔", "路数", 2);
        }

        private void SetYpCount()
        {
            IniFile inifile = new IniFile(m_path + "\\" + m_stationName + "\\yp.txt");
            inifile.Load();
            int n1 = inifile.GetInt("um71", "sum", 0) / inifile.GetInt("um71", "接收路数", 1);
            int n2 = inifile.GetInt("2000", "sum", 0) / inifile.GetInt("2000", "接收路数", 1);
            ypjsCount = n1 + n2;
            n1 = (inifile.GetInt("um71", "sum", 0) + 1) / inifile.GetInt("um71", "发送路数", 2);
            n2 = (inifile.GetInt("2000", "sum", 0) + 1) / inifile.GetInt("2000", "发送路数", 2);
            ypfsCount = n1 + n2;
        }

        private void SetDmhCount()
        {
            dmhCount = 0;
            IniFile inifile = new IniFile(m_path + "\\" + m_stationName + "\\dmh.txt");
            inifile.Load();
            int sum = inifile.GetInt("电码化", "sum", 0);
            
            for (int i = 1; i <= sum; i++)
            {
                string s = inifile.GetString("电码化", i.ToString());
                string[] ss = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (ss.Length > 1)
                {
                    dmhCount += Convert.ToInt16(ss[1]);
                }
                else
                {
                    dmhCount++;
                }
            }
           dmhCount = (dmhCount+3) / inifile.GetInt("电码化", "路数", 4);
        }

        private void SetGybdcCount()
        {
            IniFile inifile = new IniFile(m_path + "\\" + m_stationName + "\\gybdc.txt");
            inifile.Load();
            gybdcCount = (inifile.GetInt("高压不对称", "sum", 0) + 1) / inifile.GetInt("高压不对称", "路数", 2);
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.Selected)
                    {
                        cell.Value = comboBox2.Text;
                    }
                }
            }
          //  UpdateCgqCount();
        }

        private void ClearDic()
        {
            zldcbsDic.Clear();
            zldcdlDic.Clear();
            jldcbsDic.Clear();
            jldcdlDic.Clear();
            ypfsDic.Clear();
            ypjsDic.Clear();
            dmhDic.Clear();
            zlDic.Clear();
            gybdcDic.Clear();
            bzdDic.Clear();
        }

        private string GetCgqNo(DataGridViewRow row, DataGridViewCell cell)
        {
            int n1 = Convert.ToInt16(row.Cells["序号"].Value);
            string n = (n1 + 1).ToString();
            switch (cell.ColumnIndex)
            {
                case 1:
                    return "I-" + n;
                case 2:
                    return "II-" + n;
                case 3:
                    return "III-" + n;
                case 4:
                    return "IV-" + n;
            }
            return null;
        }

        private string GetCgqNo(int rowIndex, int cellIndex)
        {
            string n = (rowIndex + 2).ToString();
            switch (cellIndex)
            {
                case 1:
                    return "I-" + n;
                case 2:
                    return "II-" + n;
                case 3:
                    return "III-" + n;
                case 4:
                    return "IV-" + n;
            }
            return null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ClearDic();
            #region  把表格内的内容存入对应字典

            for (int cellIndex = 0; cellIndex < dataGridView1.Columns.Count; cellIndex++)
            {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                
                //foreach (DataGridViewCell cell in row.Cells)
                //{
                int rowIndex = row.Index;
                    if (row.Cells[cellIndex] .Value != null)
                    {
                        switch (row.Cells[cellIndex].Value.ToString())
                        {
                            case "直流道表":
                                zldcbsDic.Add(GetCgqNo(rowIndex, cellIndex), "直流道表");
                                break;
                            case "交流道表":
                                jldcbsDic.Add(GetCgqNo(rowIndex, cellIndex), "交流道表");
                                break;
                            case "直流道岔电流":
                                zldcdlDic.Add(GetCgqNo(rowIndex, cellIndex), "直流道岔电流");
                                break;
                            case "交流道岔电流":
                                jldcdlDic.Add(GetCgqNo(rowIndex, cellIndex), "交流道岔电流");
                                break;
                            case "移频发送":
                                ypfsDic.Add(GetCgqNo(rowIndex, cellIndex), "移频发送");
                                break;
                            case "移频接收":
                                ypjsDic.Add(GetCgqNo(rowIndex, cellIndex), "移频接收");
                                break;
                            case "电码化":
                                dmhDic.Add(GetCgqNo(rowIndex, cellIndex), "电码化");
                                break;
                            case "站联":
                                zlDic.Add(GetCgqNo(rowIndex, cellIndex), "站联");
                                break;
                            case "半自动":
                                bzdDic.Add(GetCgqNo(rowIndex, cellIndex), "半自动");
                                break;
                            case "高压不对称":
                                gybdcDic.Add(GetCgqNo(rowIndex, cellIndex), "高压不对称");
                                break;
                        }
                    }
                }
            }
            #endregion
            WriteStatisticsFile();
            this.Close();
        }

        private void UpdateGridFromDic()
        {
            UpdateGridFromDic(zldcbsDic);
            UpdateGridFromDic(jldcbsDic);
            UpdateGridFromDic(zldcdlDic);
            UpdateGridFromDic(jldcdlDic);
            UpdateGridFromDic(ypfsDic);
            UpdateGridFromDic(ypjsDic);
            UpdateGridFromDic(dmhDic);
            UpdateGridFromDic(bzdDic);
            UpdateGridFromDic(zlDic);
            UpdateGridFromDic(gybdcDic);
        }

        private void UpdateGridFromDic(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> pair in dic)
            {
                string[] strs = pair.Key.Split(new char[]{'-'});
                if (strs.Length == 2)
                {
                    int row = ConvertStringToInt(strs[1])-2;
                    int col = ConvertStringToInt(strs[0]);
                    this.dataGridView1.Rows[row].Cells[col].Value = pair.Value;
                }
            }
        }

        private int ConvertStringToInt(string str)
        {
            switch(str.Trim())
            {
                case "I":
                    return 1;
                case "II":
                return 2;
                case "III":
                    return 3;
                case "IV":
                    return 4;
                default:
                    return Convert.ToInt16(str.Trim());
            }
        }

        public void ReadStatisticsFile()
        {
            IniFile ini = new IniFile(m_path + "\\" + m_stationName + "\\统计.txt");
            ini.Load();
            ClearDic();
            ReadStatisticsFile(ini, "直流道岔表示", zldcbsDic);
            ReadStatisticsFile(ini, "交流道岔表示", jldcbsDic);
            ReadStatisticsFile(ini, "直流道岔电流", zldcdlDic);
            ReadStatisticsFile(ini, "交流道岔电流", jldcdlDic);
            ReadStatisticsFile(ini, "无绝缘发送", ypfsDic);
            ReadStatisticsFile(ini, "无绝缘接收", ypjsDic);
            ReadStatisticsFile(ini, "站内电码化", dmhDic);
            ReadStatisticsFile(ini, "半自动", bzdDic);
            ReadStatisticsFile(ini, "站联", zlDic);
            ReadStatisticsFile(ini, "高压不对称", gybdcDic);
        }

        private void ReadStatisticsFile(IniFile ini, string section, Dictionary<string, string> dic)
        {
            string str= ini.GetString(section, "编号");
            string[] strs = str.Split(new char[] {' ' });
            foreach (string s in strs)
            {
                dic.Add(s,ConvertString(section));
            }
        }

        private string ConvertString(string str)
        {
            switch (str)
            {
                case "直流道岔表示":
                    return "直流道表";
                case "交流道岔表示":
                    return "交流道表";
                case "无绝缘发送":
                    return "移频发送";
                case "无绝缘接收":
                    return "移频接收";
                case "站内电码化":
                    return "电码化";
                default:
                    return str;
            }
        }

        private void WriteStatisticsFile()
        {
            IniFile ini = new IniFile(m_path + "\\" + m_stationName + "\\统计.txt");
            ini.Load();
            WriteStatistics(ini, "直流道岔表示", zldcbsDic);
            WriteStatistics(ini, "交流道岔表示", jldcbsDic);
            WriteStatistics(ini, "直流道岔电流", zldcdlDic);
            WriteStatistics(ini, "交流道岔电流", jldcdlDic);
            WriteStatistics(ini, "无绝缘发送", ypfsDic);
            WriteStatistics(ini, "无绝缘接收", ypjsDic);
            WriteStatistics(ini, "站内电码化", dmhDic);
            WriteStatistics(ini, "半自动", bzdDic);
            WriteStatistics(ini, "站联", zlDic);
            WriteStatistics(ini, "高压不对称", gybdcDic);
            ini.Save();
        }

        private void WriteStatistics(IniFile ini, string section, Dictionary<string, string> dic)
        {
            ini.WriteInt(section, "数量", dic.Count);
            string str = "";
            foreach (string key in dic.Keys)
            {
                str = str + " " + key;
            }
            ini.WriteString(section, "编号", str);
        }

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            label9.Text = dataGridView1.SelectedCells.Count.ToString();
        }

        private void InitCgqCountInGrid()
        {
            d_zlbsCount = 0;
            d_zldlCount = 0;
            d_jlbsCount = 0;
            d_jldlCount = 0;
            d_ypfsCount = 0;
            d_ypjsCount = 0;
            d_dmhCount = 0;
            d_gybdcCount = 0;
            d_bzdCount = 0;
            d_zlCount = 0;

        }

        private void GetCgqCountInGrid()
        {
            InitCgqCountInGrid();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (cell.Value == null) continue;
                    switch (cell.Value.ToString())
                    {
                        case "直流道表":
                            d_zlbsCount++;
                            break;
                        case "交流道表":
                            d_jlbsCount++;
                            break;
                        case "直流道岔电流":
                            d_zldlCount++;
                            break;
                        case "交流道岔电流":
                            d_jldlCount++;
                            break;
                        case "移频发送":
                            d_ypfsCount++;
                            break;
                        case "移频接收":
                            d_ypjsCount++;
                            break;
                        case "电码化":
                            d_dmhCount++;
                            break;
                        case "站联":
                            d_zlCount++;
                            break;
                        case "半自动":
                            d_bzdCount++;
                            break;
                        case "高压不对称":
                            d_gybdcCount++;
                            break;
                    }

                }
            }
            
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            UpdateCgqCount();
        }

        private void ReadCfg_Click(object sender, EventArgs e)
        {
            ClearDic();
            ReadStatisticsFile();
            UpdateGridFromDic();
            UpdateCgqCount();
        }
    }



}
